package com.qixin.total.controller;


import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.ComCar;

import com.qixin.api.bean.ComFile;
import com.qixin.api.bean.ResultPage;
import com.qixin.api.bean.VoPage;
import com.qixin.api.service.ComCarService;

import com.qixin.api.service.ComFileService;
import jdk.nashorn.api.scripting.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.*;

/**
 * <p>
 * 租车，车辆信息 前端控制器
 * </p>
 *
 * @author 李许多
 * @since 2018-10-25
 */
@Controller
@RequestMapping("/comCar")
public class ComCarController {
    private static final Logger logger = LoggerFactory.getLogger(ComCarController.class);


    @Autowired
    ComCarService comCarService;
    @Autowired
    ComFileService comFileService;
    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("/main.html")
    public String carftl(){
        return "renzheng/car";

    }
    @RequestMapping("/car.html")
    public String car(){
        return "renzheng/carresource";

    }
    @RequestMapping("/carcheck.html")
    public String check(){
        return "/renzheng/carresourcecheck";

    }

    /**
     * 查询
     * @param voPage
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    public ResultPage<ComCar> list(VoPage voPage) {
        ResultPage<ComCar> resultPage=comCarService.selectALL(voPage);
        return  resultPage;
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public  void deleteAll(@RequestParam("ids[]")String[] ids){
        Map map = new HashMap<>();
        int i;
        for(i=0;i< ids.length;i++){
            map.put("id",ids[i]);
            comCarService.deleteByMap(map);

        }
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @RequestMapping("/updatecheck")
    @ResponseBody
    public  boolean updatecheck(String param){
        ComCar comCar = JSONObject.parseObject(param,ComCar.class);
        comCar.setGrounding(1);
        if(comCarService.updateById(comCar)){
            return  true;
        }else{
            return false;
        }
    }

    /**
     * 获取图片url
     * @param peoepleUrla
     * @param
     * @param model
     * @return
     */
    @RequestMapping("/check.html")
    public String check(String peoepleUrla, Model model,Long id){
//        System.out.println("peoepleUrla: "+peoepleUrla);
//        System.out.println("peoepleUrlb: "+peoepleUrlb);
        model.addAttribute("peoepleUrla",peoepleUrla);
       List<ComFile> comFileResultPage= comFileService.selectphoto(id);
        logger.info("本次查询出的所有车辆轮播图"+comFileResultPage.toString());
        model.addAttribute("photos",comFileResultPage );
        return "/renzheng/carresourcecheck";
    }


    /**
     *凭借url显示图片
     * @param response
     * @param path
     * @throws Exception
     */
    @RequestMapping("/getPhoto")
    public void getPhoto(HttpServletResponse response, @RequestParam("path") String path) throws Exception {
        File file = new File(path);
        FileInputStream fis;
        fis = new FileInputStream(file);
        long size = file.length();
        byte[] temp = new byte[(int) size];
        fis.read(temp, 0, (int) size);
        fis.close();
        byte[] data = temp;
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        out.write(data);
        out.flush();
        out.close();
    }

    @RequestMapping("/welcome.html")
    public String welcome(){
        return "welcome";

    }
}


